﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExpTest
{
    public partial class Frm_Speed : Form
    {
        /// <summary>
        /// 用于跨线程的设置进度值
        /// </summary>
        /// <param name="count">当前的进度值</param>
        private delegate void SetSpeedEventHandler(int count);

        /// <summary>
        /// 用于跨线程的设置状态信息
        /// </summary>
        /// <param name="msg">信息字符串</param>
        private delegate void SetMessageEventHandler(string msg);

        /// <summary>
        /// 用于跨线程关闭窗体
        /// </summary>
        private delegate void CloseFromEventHandler();

        public Frm_Speed()
        {
            InitializeComponent();
        }

        public void SetMaximum(int Maximum)
        {
            progressBar1.Maximum = Maximum;
        }

        public void SetMinimum(int Minimum)
        {
            progressBar1.Minimum = Minimum;
        }

        public void SetExportState(int count,string msg)
        {
            SetSpeed(count);
            SetStateMsg(msg);
        }

        public void CloseFrom()
        {
            if (this.InvokeRequired)
            {
                CloseFromEventHandler iCloseFrom = new CloseFromEventHandler(CloseFrom);
                this.Invoke(iCloseFrom);
            }
            else
            {
                this.Close();
                this.Dispose();
            }
        }

        private void SetSpeed(int count)
        {
            if (progressBar1.InvokeRequired)
            {
                SetSpeedEventHandler iSetSpeed = new SetSpeedEventHandler(SetSpeed);
                progressBar1.Invoke(iSetSpeed, new object[] { count });
            }
            else
            {
                if (count <= progressBar1.Maximum)
                {
                    progressBar1.Value = count;
                }
            }
        }

        private void SetStateMsg(string Msg)
        {
            if (label1.InvokeRequired)
            {
                SetMessageEventHandler iSetMsg = new SetMessageEventHandler(SetStateMsg);
                label1.Invoke(iSetMsg, new object[] { Msg });
            }
            else
            {
                label1.Text = Msg;
            }
        }
    }
}
